package com.lzy.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.lzy.dao.im.ImChatLogManager;
import com.lzy.util.IpConverter;
import com.lzy.util.Page;
import com.lzy.util.PageRequest;

@Service
@Transactional
public class MassiveQueryServiceImpl implements MassiveQueryService {

	private ImChatLogManager chatLogManager;

	private int maxNum ;
	
	@Value("${query.maxNum}")
	public void setMaxNum(int maxNum) {
		this.maxNum = maxNum;
	}
	
	@Autowired
	public void setChatLogManager(ImChatLogManager chatLogManager) {
		this.chatLogManager = chatLogManager;
	}

	public Page imChatList(Map<Object, Object> requestParams,
			PageRequest pageRequest) {

		paramsHandle(requestParams, pageRequest);

		Map<String, Object> values = new HashMap<String, Object>();

//		String sql = prepareSql(requestParams, pageRequest, values);
//
//		Page ims = this.chatLogManager.queryListBySql(sql, values, pageRequest);
		
		String sql = generateWhereCondition(requestParams, pageRequest, values);
		
		Page ims = this.chatLogManager.findPage(sql, values, pageRequest);
		
		return ims;
	}
	
	private String generateWhereCondition(Map<Object, Object> requestParams,
			PageRequest pageRequest, Map<String, Object> values) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" where  1=1 ");

		String startTime = MapUtils.getString(requestParams, "startTime");
		if (StringUtils.hasText(startTime)) {
			String startDateTime = startTime+" "+MapUtils.getIntValue(requestParams, "startTime_hour")+":"+
			MapUtils.getIntValue(requestParams, "startTime_minute")+":"+
			MapUtils.getIntValue(requestParams, "startTime_second");
			
			buffer.append(" and CaptureTime >= to_date('"+startDateTime +"','yyyy-MM-dd hh24:mi:ss')");
		}

		String endTime = MapUtils.getString(requestParams, "endTime");
		if (StringUtils.hasText(endTime)) {
			String endDateTime = endTime+" "+(MapUtils.getIntValue(requestParams, "endTime_hour"))+":"+
								MapUtils.getIntValue(requestParams, "endTime_minute")+":"+
								MapUtils.getIntValue(requestParams, "endTime_second");
			
			buffer.append(" and CaptureTime <= to_date('"+endDateTime +"','yyyy-MM-dd hh24:mi:ss')");
		}

		String logName = MapUtils.getString(requestParams, "loginname");
		if (StringUtils.hasText(logName)) {
			buffer.append(" and( loginName = '" + logName + "' or  SrcID='" + logName +"' or DestID='"+ logName +"' )  ");
		}

		String keyword = MapUtils.getString(requestParams, "keyword");
		if (StringUtils.hasText(keyword)) {
			buffer.append(" and content like '%" + keyword + "%' ");
		}

		String ip = MapUtils.getString(requestParams, "ip");
		if (StringUtils.hasText(ip)) {
			long ipLong = IpConverter.ipToLong(ip);
			buffer.append(" and (ClientIP =" + ipLong + " or ServerIP = " + ipLong
					+ " )");
		}

		String pType = MapUtils.getString(requestParams, "pType");
		if (StringUtils.hasText(pType)) {
			buffer.append(" and IMtype in( " + pType + ")");
		}

		String actionType = MapUtils.getString(requestParams, "actionType");
		if (StringUtils.hasText(actionType)) {
			buffer.append(" and type in(  " + actionType + " )");
		}
		
		buffer.append(" and rownum<="+ maxNum);
		
		return buffer.toString();
	}

	private String prepareSql(Map<Object, Object> requestParams,
			PageRequest pageRequest, Map<String, Object> values) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(" select * from TBIM ");
		buffer.append(" where  1=1 ");

		String startTime = MapUtils.getString(requestParams, "startTime");
		if (StringUtils.hasText(startTime)) {
			String startDateTime = startTime+" "+MapUtils.getIntValue(requestParams, "startTime_hour")+":"+
			MapUtils.getIntValue(requestParams, "startTime_minute")+":"+
			MapUtils.getIntValue(requestParams, "startTime_second");
			
			buffer.append(" and CaptureTime >= to_date('"+startDateTime +"','yyyy-MM-dd hh24:mi:ss')");
		}

		String endTime = MapUtils.getString(requestParams, "endTime");
		if (StringUtils.hasText(endTime)) {
			String endDateTime = endTime+" "+(MapUtils.getIntValue(requestParams, "endTime_hour"))+":"+
								MapUtils.getIntValue(requestParams, "endTime_minute")+":"+
								MapUtils.getIntValue(requestParams, "endTime_second");
			
			buffer.append(" and CaptureTime <= to_date('"+endDateTime +"','yyyy-MM-dd hh24:mi:ss')");
		}

		String logName = MapUtils.getString(requestParams, "loginname");
		if (StringUtils.hasText(logName)) {
			buffer.append(" and( loginName = '" + logName + "' or  SrcID='" + logName +"' or DestID='"+ logName +"' )  ");
		}

		String keyword = MapUtils.getString(requestParams, "keyword");
		if (StringUtils.hasText(keyword)) {
			buffer.append(" and content like '%" + keyword + "%' ");
		}

		String ip = MapUtils.getString(requestParams, "ip");
		if (StringUtils.hasText(ip)) {
			long ipLong = IpConverter.ipToLong(ip);
			buffer.append(" and (ClientIP =" + ipLong + " or ServerIP = " + ipLong
					+ " )");
		}

		String pType = MapUtils.getString(requestParams, "pType");
		if (StringUtils.hasText(pType)) {
			buffer.append(" and IMtype in( " + pType + ")");
		}

		String actionType = MapUtils.getString(requestParams, "actionType");
		if (StringUtils.hasText(actionType)) {
			buffer.append(" and type in(  " + actionType + " )");
		}
		
		buffer.append(" and rownum<="+ maxNum );

		return buffer.toString();
	}

	private void paramsHandle(Map<Object, Object> params,
			PageRequest pageRequest) {
		if (params.containsKey("start") && params.get("start") != null) {
			pageRequest.setStart(Integer
					.valueOf(params.get("start").toString()));
		}

		if (params.containsKey("limit") && params.get("limit") != null) {
			pageRequest.setLimit(Integer
					.valueOf(params.get("limit").toString()));
		}
	}

}
